<template>
	<u-popup 
		:bgColor="bgColor" 
		:show="show" 
		:overlay="overlay" 
		:mode="mode" 
		:closeOnClickOverlay="closeOnClickOverlay"
		@close="$emit('close')" 
		@open="$emit('open')">
		<view v-if="type == 'modal'" class="modal" :style="{width}">
			<view class="title">
				<text>{{title}}</text>
				<view class="close" @click.stop="close">
					<u-icon name="close-circle-fill" color="#bbbbbb" size="48rpx"></u-icon>
				</view>
			</view>
			<!-- 插槽内容 -->
			<slot></slot>
		</view>
		<view v-else-if="type == 'confirm'" class="confirm">
			<view class="title">{{title}}</view>
			<view class="content">
				{{content}}
			</view>
			<view class="btns">
				<view class="btn-item" @click.stop="cancel">
					<custom-button type="info" shape>{{cancelText}}</custom-button>
				</view>
				<view class="btn-item" @click.stop="confirm">
					<custom-button type="primary" shape>{{confirmText}}</custom-button>
				</view>
			</view>
		</view>
	</u-popup>
</template>

<script>
	export default {
		name:"modal-box",
		props: {
			type: { // 类型
				type: String,
				default: 'modal'
			},
			title: {
				type: String,
				default: '标题'
			},
			width: {
				type: String,
				default: '600rpx'
			},
			content: {
				type: String,
				default: '操作成功！'
			},
			overlay: { // 是否显示遮罩
				type: Boolean,
				default: true
			},
			mode: { // 弹出方向
				type: String,
				default: 'center'
			},
			bgColor: {
				type: String,
				default: 'transparent'
			},
			closeOnClickOverlay: { // 点击遮罩是否关闭弹窗
				type: Boolean,
				default: true
			},
			confirmText: {
				type: String,
				default: '确认'
			},
			cancelText: {
				type: String,
				default: '取消'
			}
		},
		data() {
			return {
				show: false
			};
		},
		methods: {
			open(){
				this.show = true;
			},
			close(){
				this.show = false;
			},
			confirm(){
				this.$emit('confirm');
				this.close();
			},
			cancel(){
				this.$emit('cancel');
				this.close();
			}
		}
	}
</script>

<style lang="scss" scoped>
.modal {
	border-radius: 20rpx;
	background-color: #fff;
	overflow: hidden;
	.title{
		position: relative;
		display: flex;
		align-items: center;
		justify-content: center;
		height: 80rpx;
		color: #333;
		font-size: $fs-30;
		border-bottom: 2rpx solid #dddddd;
		.close{
			position: absolute;
			top: 50%;
			transform: translateY(-50%);
			right: 20rpx;
		}
	}
}
.confirm {
	width: 600rpx;
	border-radius: 20rpx;
	background-color: #fff;
	padding: 30rpx 40rpx;
	.title{
		color: #333;
		font-size: $fs-30;
		margin-bottom: 20rpx;
	}
	.content{
		min-height: 100rpx;
		font-size: $fs-26;
		padding: 20rpx;
	}
	.btns{
		display: flex;
		align-items: center;
		justify-content: space-between;
		.btn-item{
			flex: 1;
		}
		.btn-item:first-child{
			margin-right: 40rpx;
		}
	}
}
</style>